package com.ruoyi.web.cp.service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.web.core.config.ConfigLoader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Slf4j
@Service
public class HttpService {
    @Autowired
    private ConfigLoader configLoader;



    /**
     * @Description: 获取fb列表
     * @Author: xiesm
     * @Date: 2025/3/21
     */
    public JSONObject sendFbList(Integer current) {
        JSONObject body = new JSONObject();
        body.put("current", current);
        body.put("size", 100);
        body.put("sportId", 1);
        body.put("languageType", "CMN");
        body.put("orderBy", 0);
        body.put("oddsType", 1);
        body.put("isPC", true);
        body.put("type", "3");
        JSONObject res = HttpUtils.httpJson("https://yapi.weilaiyy2.com/v1/match/getList", "post", body, null);
        return res;
    }

    /**
     * @Description: 获取fb列表
     * @Author: xiesm
     * @Date: 2025/3/21
     */
    public JSONObject sendFbDetail(String mid, String game) {
        String filePath = configLoader.getRmPath() + "fb-gamescore/" + mid + ".txt";
        boolean fileExist = FileUtils.isFileExist(filePath);
        if (fileExist) {
            String text = FileUtil.readString(filePath, CharsetUtil.charset(("utf8")));
            if (text.length() > 100) {
                return JSONObject.parseObject(text);
            }
        }
        log.info(game);
        JSONObject body = new JSONObject();
        body.put("languageType", "CMN");
        body.put("oddsType", 1);
        body.put("matchId", mid);
        JSONObject result = HttpUtils.httpJson("https://yapi.weilaiyy2.com/v1/match/getMatchDetail", "post", body, null);
        FileUtil.writeString(result.toJSONString(), configLoader.getRmPath() + "fb-gamescore\\" + mid + ".txt", "utf8");
        ThreadUtil.sleep(50);
        return result;
    }

    /**
     * @Description: ao8
     * @Author: xiesm
     * @Date: 2025/4/22
     */
    public JSONObject sendAo8List() {
        JSONObject result = HttpUtils.httpJson("https://api.api168168.com/klsf/queryDrawCodeTrend.do?periods=30&date=&lotCode=10011", "get", null, null);
        return result;
    }

    /**
     * @Description: ao8
     * @Author: xiesm
     * @Date: 2025/4/22
     */
    public JSONObject sendAo8Yilou() {
        JSONObject result = HttpUtils.httpJson("https://api.api168168.com/klsf/queryToDayNumberLawOfStatistics.do?lotCode=10011", "get", null, null);
        return result;
    }

    /**
     * @Description: ao10
     * @Author: xiesm
     * @Date: 2025/4/22
     */
    public JSONObject sendAo10List() {
        return sendAo10List(null);
    }

    public JSONObject sendAo10List(String date) {
        String url = "https://api.api168168.com/pks/getPksHistoryList.do?lotCode=10012&date=" + (date != null ? date : "");
        return HttpUtils.httpJson(url, "get", null, null);
    }

    public JSONObject getLeyuHeaders() {
        List<String> cookie = FileUtil.readLines(configLoader.getGamenamesPath() + "leyu-cookie.txt", "utf8");
        JSONObject header = new JSONObject();
        header.put("token", cookie.get(0));
        return header;
    }

    public JSONObject sendLeyuCpList(String ticketId, String num) {
        JSONObject header = getLeyuHeaders();
        String url = StrUtil.format("https://cgcpapiyo2p4.n27jpr.com:10151/coron/trendGraph/chart/history?num={}&ticketId={}", num, ticketId);
        return HttpUtils.httpJson(url, "get", null, header);
    }

    public JSONObject sendLeyuCpList(String ticketId) {
        return sendLeyuCpList(ticketId, "10");
    }
}
